<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Enter description here ...
 * 
 * @author Mark Phillip Johnson
 * @version 1.0
 * @copyright Copyright (c) 2010, markjohnson
 * @license http://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0
 *
 */
class Storage extends Object
{
	private static $instance = NULL;
	
	var $conf = NULL;

	/**
	 * 
	 */
	public function __construct()
	{
		self::$instance = & $this;
	}

	public static function &getInstance()
	{
		if (!isset(self::$instance))
		{
			new Storage();
		}
		
		return self::$instance;
	}

	/**
	 * cache_set()
	 *
	 * @param string $cid
	 * @param mixed $data
	 * @param string $table
	 * @param define $expire
	 * @param string $header
	 */
	public static function cacheSet($cid, $data, $table = 'cache', $expire = CACHE_PERMANENT, $header = NULL)
	{
		$serialized = 0;
		if (is_object($data) || is_array($data))
		{
			$data = serialize($data);
			$serialized = 1;
		}
		
		$created = time();
		Db::query("UPDATE " . $table . " SET data = %b, created = %d, expire = %d, headers = '%s', serialized = %d WHERE cid = '%s'", $data, $created, $expire, $header, $serialized, $cid);
		
		if (!Db::affectedRows())
		{
			@Db::query("INSERT INTO " . $table . " (cid, data, created, expire, headers, serialized) VALUES ('%s', %b, %d, %d, '%s', %d)", $cid, $data, $created, $expire, $header, $serialized);
		}
	}

	/**
	 * cache_get()
	 *
	 * TODO: finish this function
	 *
	 * @param string $cid
	 * @param string $table
	 */
	public static function cacheGet($cid, $table = 'cache')
	{
		global $user;
		
		$cache = Db::fetchObject(Db::query("SELECT data, created, headers, expire, serialized FROM " . $table . " WHERE cid = '%s'", $cid));
		if (isset($cache->data))
		{
			if ($cache->expire == CACHE_PERMANENT || !Storage::get('cache_lifetime', 0))
			{
				if ($cache->serialized)
				{
					$cache->data = unserialize($cache->data);
				}
			}
			else
			{
				if ($user->cache > $cache->created)
				{
					return 0;
				}
				else
				{
					$cache->data = unserialize($cache->data);
				}
			}
		}
		
		return $cache;
	}

	/**
	 *
	 * @param mixed $conf
	 */
	public static function init($conf = array())
	{
		if (($cached = Storage::cacheGet('variables', 'cache')) !== FALSE)
		{
			$variables = $cached->data;
		}
		else
		{
			$variables = array();
			$result = Db::query('SELECT * FROM variable');
			while (($variable = Db::fetchObject($result)) !== FALSE)
			{
				$variables[$variable->name] = unserialize($variable->value);
			}
			
			Storage::cacheSet('variables', $variables);
		}
		
		foreach ($conf as $name => $value)
		{
			$variables[$name] = $value;
		}
		
		$_this = & Storage::getInstance();
		$_this->conf = $variables;
	}

	/**
	 *
	 * @param string $name
	 * @param mixed $default
	 * @return mixed
	 */
	public static function get($name, $default)
	{
		$_this = & Storage::getInstance();
		return isset($_this->conf[$name]) ? $_this->conf[$name] : $default;
	}

	/**
	 *
	 * @param string $name
	 * @param mixed $value
	 */
	public static function set($name, $value)
	{
		$serialized = serialize($value);
		Db::query("UPDATE variable SET value = '%s' WHERE name = '%s'", array(
			$serialized, 
			$name
		));
		if (!Db::affectedRows())
		{
			@Db::query("INSERT INTO variable (name, value) VALUES ('%s', '%s')", array(
				$name, 
				$serialized
			));
		}
		
		$_this = & Storage::getInstance();
		$_this->conf[$name] = $value;
	}

	public static function delete($name)
	{
		Db::query("DELETE FROM variable WHERE name = '%s'", $name);
		// cache clear all for variables 
		

		$_this = & Storage::getInstance();
		unset($_this->conf[$name]);
	}
}
